package ATM_mysql;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuPanel extends JPanel implements ActionListener {
    private ATM frame;
    private JButton searchBtn = new JButton(Util.getIcon("/images/buttonFind.jpg"));
    private JButton saveBtn = new JButton(Util.getIcon("/images/buttonSave.jpg"));
    private JButton getBtn = new JButton(Util.getIcon("/images/buttonGet.jpg"));
    private JButton transferBtn = new JButton("转账");
    private JButton modifyBtn = new JButton(Util.getIcon("/images/buttonModify.jpg"));
    private JButton exitBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));

    public MenuPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        searchBtn.setBounds(0, 100, 140, 50);
        searchBtn.addActionListener(this);
        this.add(searchBtn);

        saveBtn.setBounds(900, 100, 140, 50);
        saveBtn.addActionListener(this);
        this.add(saveBtn);

        getBtn.setBounds(0, 250, 140, 50);
        getBtn.addActionListener(this);
        this.add(getBtn);

        transferBtn.setBounds(900, 250, 140, 50);
        transferBtn.addActionListener(this);
        this.add(transferBtn);

        modifyBtn.setBounds(0, 400, 140, 50);
        modifyBtn.addActionListener(this);
        this.add(modifyBtn);

        exitBtn.setBounds(900, 400, 140, 50);
        exitBtn.addActionListener(this);
        this.add(exitBtn);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == searchBtn) {
            DBUtil db = new DBUtil();
            User user = db.queryById(frame.getLoginUser().getId());
            frame.getSearchPanel().setbalance(user.getBalance());
            frame.switchPanel("search");
        } else if (e.getSource() == saveBtn) {
            frame.switchPanel("save");
        } else if (e.getSource() == getBtn) {
            frame.switchPanel("get");
        } else if (e.getSource() == transferBtn) {
            frame.switchPanel("transfer");
        } else if (e.getSource() == exitBtn) {
            frame.switchPanel("login");
        }else if (e.getSource() == modifyBtn) {
            frame.switchPanel("modify");
        }
    }
}
